package com.yz.promotion.controller;

import com.yz.api.facade.promotion.PromotionFacade;
import com.yz.model.vo.promotion.PromotionVo;
import com.yz.common.resp.BaseResponse;
import com.yz.promotion.service.PromotionService;
import com.yz.common.utils.ResultUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * <p>
 * User:Mr.Du
 * Date:2024/3/25
 * Time:16:17
 */
@Slf4j
@RestController
public class PromotionController implements PromotionFacade {

    @Resource
    private PromotionService promotionService;

    @Override
    public BaseResponse createPromotionCode(Long uid) {
        if(StringUtils.isBlank(uid+"")){
            throw new RuntimeException("参数不能为空");
        }
        promotionService.createPromotionCode(uid);
        return ResultUtils.success("成功");
    }

    @Override
    public BaseResponse<PromotionVo> checkPromotionCode(Long uid, String codes) {
        if(StringUtils.isAnyBlank(uid.toString(), codes)){
            throw new RuntimeException("参数不能为空");
        }
        PromotionVo promotionVo = promotionService.checkPromotionCode(uid, codes);
        if(promotionVo == null){
            throw new RuntimeException("优惠码不可用");
        }

        return ResultUtils.success("优惠码可用", promotionVo);
    }

    @Override
    public BaseResponse<List<PromotionVo>> getPromotionCode(Long uid, Integer status, Integer type) {
        if(StringUtils.isBlank(uid.toString())){
            throw new RuntimeException("参数不能为空");
        }
        List<PromotionVo> res = promotionService.getPromotionCode(uid, status, type);
        return ResultUtils.success(res);
    }

    @Override
    public BaseResponse<Boolean> usePromotionCode(Long uid, String codes) {
        if(StringUtils.isAnyBlank(uid.toString(), codes)){
            throw new RuntimeException("参数不能为空");
        }
        promotionService.usePromotionCode(uid, codes);
        return ResultUtils.success(true);
    }
}
